package com.chang.soul.conf;

import java.util.ArrayList;
import java.util.List;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置
 * @author xiaoqingyang@vip.henu.edu.cn
 * @date 2021/3/24 21:48
 */
@Configuration
@EnableSwagger2 // 开启 swagger2 功能
public class SwaggerConfig {
	private Boolean			swaggerEnabled=true;
	@Bean
	public Docket restApiDocket() {
		ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_2) //
				.apiInfo(apiInfo())
				.enable(swaggerEnabled) // 是否开启
				.genericModelSubstitutes(DeferredResult.class) //
				.useDefaultResponseMessages(false) //
				.forCodeGeneration(false) //
				.pathMapping("/").select()

				.apis(RequestHandlerSelectors.basePackage("com.chang.soul.controller")) // 指定扫描的包路径
				.paths(PathSelectors.any())
				.build()//.securitySchemes(security() ).pathMapping("/");
				.globalOperationParameters(pars).pathMapping("/");
	}


	private List<ApiKey> security() {
	List<ApiKey>	list = new ArrayList<ApiKey>();
			list.add(new ApiKey("token", "token", "header"));
        return list;
        }

	/**
	 * @方法描述:构建 api文档的详细信息函数
	 * @return
	 */
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("心灵解忧铺后台数据接口")
				.description("一级路径/soul，二级路径必须模块名/菜单名(user(表用户管理界面))后面设置三级路径为具体操作,需要先访问login 获取Token认证 才可以调取数据;")
				.version("0.0.1")
				.build();
	}
}
